package com.example.demoserver.controller;

import com.example.demoserver.entity.InteAppstate;
import com.example.demoserver.entity.InteGateway;
import com.example.demoserver.entity.InteGatewaylog;
import com.example.demoserver.service.InteGatewaylogService;
import com.example.demoserver.service.impl.InteAppstateServiceImpl;
import com.example.demoserver.service.impl.InteGatewayServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/gateway")
@RequiredArgsConstructor
@Component
public class InteGatewayController {
    private final InteGatewayServiceImpl inteGatewayService;
    private final InteGatewaylogService inteGatewaylogService;

    @GetMapping("/selectall")
    public List<InteGateway> selectAll () {
        List<InteGateway> list=inteGatewayService.selectAll();
        return list;
    }
    @GetMapping("/delete")
    public Integer deleteById (Integer id) {
        InteGateway inteGateway=inteGatewayService.selectById(id);
        saveLog("删除","Get","删除了设备"+inteGateway.getGatewayname());
        Integer bol=inteGatewayService.deleteById(id);
        return bol;
    }
    @PostMapping("/insert")
    public InteGateway insertGateway (@RequestBody InteGateway inteGateway) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatted = now.format(formatter);
        inteGateway.setCreatetime(formatted);
        inteGatewayService.save(inteGateway);
        saveLog("新增","Post","新增了设备"+inteGateway.getGatewayname());
        return inteGateway;
    }
    @PostMapping("/update")
    public boolean updateGateway (@RequestBody InteGateway inteGateway) {
        InteGateway inteGateway1=inteGatewayService.selectById(inteGateway.getId());
        saveLog("修改","Post","修改了设备"+inteGateway1.getGatewayname()
                + "的信息:设备id "+inteGateway.getGatewayid()
                +",设备名称 "+inteGateway.getGatewayname()
                +",设备密码 "+inteGateway.getMqttpassword()
                +",设备介绍 "+inteGateway.getIntroduce()
        );
        boolean bol=inteGatewayService.updateById(inteGateway);
        return bol;
    }
    @GetMapping("/reset")
    public InteGateway resetPassword (Integer id) {
        InteGateway inteGateway=new InteGateway();
        inteGateway.setId(id);
        String pas=getRandom();
        inteGateway.setMqttpassword(pas);
        inteGatewayService.updateById(inteGateway);

        InteGateway inteGateway1=inteGatewayService.selectById(inteGateway.getId());
        saveLog("重置","Get","重置了设备"+inteGateway1.getGatewayname()+"的密码为"+pas);
        return inteGateway;
    }
    @GetMapping("/detail")
    public InteGateway selectOne (String gatewayId) {
        InteGateway inteGateway=inteGatewayService.selectByGatewayId(gatewayId);
        return inteGateway;
    }
    public String getRandom() {
        String charPool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz|<>?/(){}[]";
        StringBuilder str = new StringBuilder();
        Random rnd = new Random();
        while (str.length() < 24) { // length of the random string.
            int index = (int) (rnd.nextFloat() * charPool.length());
            str.append(charPool.charAt(index));
        }
        String finalString = str.toString();
        return finalString;
    }

    public void saveLog(String op,String type,String value){
        InteGatewaylog inteGatewaylog=new InteGatewaylog();
        inteGatewaylog.setHtype(type);
        inteGatewaylog.setLogoperate(op);
        inteGatewaylog.setLogvalue(value);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatted = now.format(formatter);
        inteGatewaylog.setLogtime(formatted);
        inteGatewaylogService.save(inteGatewaylog);
    }
}
